﻿using System.Windows ;
using System.Windows.Controls ;
using System.Windows.Interactivity ;
using System.ComponentModel ;

namespace OpenLightGroup.Common.Interactivity
    {

        /// <summary>
        /// Automatically select all text in the associated TextBox when it gets focus.
        /// </summary>
        [Description( "Automatically select all text in the associated TextBox when it gets focus." )]
        public class SelectTextOnFocusBehavior
        : Behavior<TextBox>
            {
                /// <summary>
                /// Overrides the base Behavior class method. This method wires up the event handler to the associated TextBox's GotFocus event.
                /// </summary>
                protected override void OnAttached ( )
                    {
                        base.OnAttached( ) ;
                        this.AssociatedObject.GotFocus += new RoutedEventHandler( AssociatedObject_GotFocus ) ;
           
                    }

                /// <summary>
                /// Overrides the base Behavior class method. This method removes the event handler to the associated TextBox's GotFocus event.
                /// </summary>
                protected override void OnDetaching ( )
                    {
                        base.OnDetaching( ) ;

                        this.AssociatedObject.GotFocus -= AssociatedObject_GotFocus ;

                    }

                private void AssociatedObject_GotFocus ( object sender, RoutedEventArgs e )
                    {

                        this.AssociatedObject.SelectAll( ) ;

                    }
            }
    }